package com.yinhe.music.yhmusic.personal.modify;

import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.hwangjr.rxbus.RxBus;
import com.yinhe.music.common.GlideHelper.GlideHelper;
import com.yinhe.music.common.view.RoundedImageView;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.event.RxbusNullObject;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseTakePhotoActivity;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.constants.KeyConstants;
import com.yinhe.music.yhmusic.dialog.CameraDialog;
import com.yinhe.music.yhmusic.utils.Preferences;
import com.yinhe.music.yhmusic.utils.ToastUtils;
import com.yinhe.music.yhmusic.utils.UmengEventUtils;

import java.io.File;

import static com.yinhe.music.yhmusic.constants.IConstants.IMAGE_PATH;
import static com.yinhe.music.yhmusic.constants.IConstants.MODIFY_USER_INFO;

public class ModifyActivity extends BaseTakePhotoActivity implements ModifyContract.IModifyView {
    private FrameLayout head;
    private FrameLayout sex;
    private FrameLayout name;
    private TextView tv_name;
    private TextView tv_sex;
    private RoundedImageView image;
    private EditText mNameEdText;
    private TextView tvNameTitle;
    private TextView tvImageTitle;

    private int mGender;
    private String imageUrl;
    private String mName;
    private int modifyType;
    private String id;
    String[] items = {"男", "女"};

    ModifyPresenter mPresenter;

    @Override
    public void initView() {
        head = findViewById(R.id.head_image);
        sex = findViewById(R.id.sex);
        name = findViewById(R.id.name);
        tv_name = findViewById(R.id.tv_nickname);
        tv_sex = findViewById(R.id.tv_sex);
        image = findViewById(R.id.image);
        tvNameTitle = findViewById(R.id.tv_name_title);
        tvImageTitle = findViewById(R.id.tv_image_title);

        if (getIntent() != null) {
            mGender = getIntent().getIntExtra(KeyConstants.KEY_GENDER, 0);
            mName = getIntent().getStringExtra(KeyConstants.KEY_NAME);
            imageUrl = getIntent().getStringExtra(KeyConstants.KEY_IMAGE);
            modifyType = getIntent().getIntExtra(KeyConstants.KEY_MODIFY_TYPE, IConstants.MODIFY_USER_INFO);
            id = getIntent().getStringExtra(KeyConstants.KEY_MODIFY_ID);
        }

        showQuickControl(true);
        setListener();
        initUI();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_my_info;
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new ModifyPresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    private void initUI() {
        if (modifyType == IConstants.MODIFY_USER_INFO) {
            setToolbar("我的资料");
            GlideHelper.setCircleImageResource(image, imageUrl, R.drawable.default_cover);
            if (mGender == 0) {
                tv_sex.setText("男");
            } else {
                tv_sex.setText("女");
            }
        } else {
            setToolbar("编辑歌单信息");
            sex.setVisibility(View.GONE);
            GlideHelper.setImageResource(image, imageUrl, R.drawable.default_cover);
            tvImageTitle.setText("更换封面");
            tvNameTitle.setText("名称");
        }

        if (mName != null) {
            tv_name.setText(mName);
        }
    }

    private void setListener() {
        head.setOnClickListener(view -> showChangeHeadDialog());
        sex.setOnClickListener(view -> showChangeSexDialog());
        name.setOnClickListener(view -> showChangeNameDialog());
    }

    public void showChangeHeadDialog() {
        CameraDialog cameraDialog = new CameraDialog(this, new CameraDialog.TakePhotoListener() {
            @Override
            public void didTakePhoto() {
                takePhoto();
            }

            @Override
            public void didSelectPhoto() {
                getPhoto();
            }
        });
        cameraDialog.show();
    }

    private void showChangeSexDialog() {
        AlertDialog.Builder dialog = new AlertDialog.Builder(ModifyActivity.this);
        dialog.setTitle("性别");
        dialog.setItems(items, (dialogInterface, i) -> {
            mGender = i;
            mPresenter.modifyInfo(modifyType, null, null, String.valueOf(i), null);
        });
        dialog.show();
    }

    private void showChangeNameDialog() {
        mNameEdText = new EditText(this);
        AlertDialog.Builder dialog = new AlertDialog.Builder(this);
        dialog.setTitle("输入名称").setView(mNameEdText);
        dialog.setPositiveButton("确定", (dialogInterface, i) -> {
            mName = mNameEdText.getText().toString();
            if (mName.isEmpty()) {
                ToastUtils.show("请输入正确的字符");
            } else {
                if (modifyType == IConstants.MODIFY_USER_INFO) {
                    mPresenter.modifyInfo(modifyType, null, mName, null, null);
                } else {
                    mPresenter.modifyInfo(modifyType, id, null, null, mName);
                }
            }
        });
        dialog.setNegativeButton("取消", null);
        dialog.show();
    }

    @Override
    public void updateImageUI() {
        File file = new File(IMAGE_PATH, fileName);
        GlideHelper.setCircleImageFile(this, file, image);
        if (modifyType == MODIFY_USER_INFO) {
            UmengEventUtils.editUserInfo(ModifyActivity.this, UmengEventUtils.USER_IMAGE);
            RxBus.get().post(RxBusEventType.User.UPDATE_USER_INFO, RxbusNullObject.INSTANCE);
        } else {
            RxBus.get().post(RxBusEventType.SongMenu.UPDATE_MENU_IMAGE, RxbusNullObject.INSTANCE);
        }

    }

    @Override
    public void updateInfoUI() {
        if (modifyType == MODIFY_USER_INFO) {
            tv_sex.setText(items[mGender]);
            Preferences.setSex(items[mGender]);
            RxBus.get().post(RxBusEventType.User.UPDATE_USER_INFO, RxbusNullObject.INSTANCE);
        } else {
            RxBus.get().post(RxBusEventType.SongMenu.UPDATE_MENU_NAME, RxbusNullObject.INSTANCE);
        }
        tv_name.setText(mName);
    }

    @Override
    public void updateImage(File file) {
        mPresenter.modifyImage(file, modifyType, id);
    }
}
